from ast import Break
import signal
from choose_music import get_random_music
from music_play import play_random_segment_of_music
import _thread
import os
import inspect
import ctypes
import re


def stop_thread(tid, exctype=SystemExit):
    """raises the exception, performs cleanup if needed"""
    if not inspect.isclass(exctype):
        exctype = type(exctype)
    res = ctypes.pythonapi.PyThreadState_SetAsyncExc(tid, ctypes.py_object(exctype))
    if res == 0:
        # raise ValueError("invalid thread id")
        pass
    elif res != 1:
        # """if it returns a number greater than one, you're in trouble,
        # and you should call it again with exc=NULL to revert the effect"""
        ctypes.pythonapi.PyThreadState_SetAsyncExc(tid, None)
        raise SystemError("PyThreadState_SetAsyncExc failed")
        
music_length = 20000
music_length_level = [100,200,500,1000,2000,5000,10000,20000]
music_level = 7
quit_flag = False
while True:
    if quit_flag:
        break
    file_name, music_name = get_random_music()
    start_time, thread_id = play_random_segment_of_music(file_name, segment_length=music_length)
    # print("thread_id 为",thread_id)
    max_guess_time = 5
    guess_count = 0
    while True:
        name = input("请输入歌名，再播放一遍请输入r或replay，播放该歌曲其他片段请输入o或other，跳过该歌曲请输入s或者skip，结束游戏请输入q或者quit，增大难度请输入+，降低难度请输入-（均可一次输入多个）\n")
        if name == 'r' or name == 'replay':
            stop_thread(thread_id)
            start_time, thread_id = play_random_segment_of_music(file_name, segment_length=music_length, start_time=start_time)
        elif name == 's' or name == 'skip':
            stop_thread(thread_id)
            print("该歌已跳过，这首歌歌名是《%s》"%music_name)
            break
        elif name == 'o' or name == 'other':
            stop_thread(thread_id)
            start_time, thread_id = play_random_segment_of_music(file_name, segment_length=music_length)
        elif name == 'q' or name == 'quit':
            stop_thread(thread_id)
            quit_flag = True
            break
        elif name == '+' or re.fullmatch('\++',name):
            stop_thread(thread_id)
            music_level -= len(name)
            if music_level < 0:
                music_level = 0
            music_length = music_length_level[music_level]
            start_time, thread_id = play_random_segment_of_music(file_name, segment_length=music_length)
        elif name == '-' or re.fullmatch('\-+',name):
            stop_thread(thread_id)
            music_level += len(name)
            if music_level > len(music_length_level)-1:
                music_level = len(music_length_level)-1
            music_length = music_length_level[music_level]
            start_time, thread_id = play_random_segment_of_music(file_name, segment_length=music_length)
        else:
            if name == music_name:
                stop_thread(thread_id)
                print("猜对啦！歌名就是《%s》"%name)
                break
            else:
                print("猜错啦，再猜一次吧")
                guess_count += 1
                if guess_count == max_guess_time:
                    print("很遗憾，%d次都没有猜对！歌名是《%s》"%(max_guess_time, music_name))
                    break
                